package com.yksj.healthtalk.comm;

import java.util.ArrayList;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.SearchAdapter;
import com.yksj.healthtalk.entity.BaseInfoEntity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.chatting.ChatActivity;
import com.yksj.healthtalk.ui.friend.FriendHttpListener;
import com.yksj.healthtalk.ui.friend.FriendInformUI;
import com.yksj.healthtalk.ui.salon.SalonInformationActivity;
import com.yksj.healthtalk.ui.salon.SalonReadSelf;
import com.yksj.healthtalk.ui.salon.SalonSelectPaymentOptionActivity.OnBuyTicketHandlerListener;
import com.yksj.healthtalk.utils.FriendHttpUtil;
import com.yksj.healthtalk.utils.SalonHttpUtil;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 标签搜索界面
 * @author Administrator
 * 
 */
public class SearchByTagActivity extends BaseFragmentActivity implements
		OnItemClickListener, OnClickListener,
		FriendHttpListener, 
		OnBuyTicketHandlerListener {

	private PullToRefreshListView mPullToRefreshListView;
	private ListView lv;
	private GroupInfoEntity mGroupInfoEntity;
	private String flagPlacing = "-100000";
	private String Flag = "";
	private ArrayList<CustomerInfoEntity> mCustomerInfoEntities;
	private ArrayList<GroupInfoEntity> mGroupInfoEntities;
	private SearchAdapter<? extends BaseInfoEntity> mAdapter;
	private Object object;
	private CustomerInfoEntity mCustomerInfoEntity;
	private LodingFragmentDialog showLodingDialog;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.search_tag_layout);
		initWidget();
		initData(getIntent());

		mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

					@Override
					public void onRefresh(PullToRefreshBase<ListView> refreshView) {
						if (object instanceof GroupInfoEntity) {
							mGroupInfoEntity.setFlag(Flag);
							mGroupInfoEntity.setFlagPlacing(flagPlacing);
//							SalonHttpUtil.getSalonInfo(mGroupInfoEntity, 0,SearchByTagActivity.this);
							SalonHttpUtil.getSalonInfoData(mGroupInfoEntity, 0,responseHandler);
						} else {
							mCustomerInfoEntity.setFlag(Flag);
							FriendHttpUtil.getFriends(mCustomerInfoEntity, 0,SearchByTagActivity.this);
						}

					}
				});
	}

	private void initData(Intent intent) {
		titleTextV.setTextColor(Color.BLACK);
		titleLeftBtn.setText(R.string.back);
		titleLeftBtn.setOnClickListener(this);

		object = intent.getExtras().get("entity");
		showLodingDialog = LodingFragmentDialog.showLodingDialog(getSupportFragmentManager(),getResources());
		if (object instanceof CustomerInfoEntity) {
			mCustomerInfoEntities = new ArrayList<CustomerInfoEntity>();
			mAdapter = new SearchAdapter<CustomerInfoEntity>(this,
					mCustomerInfoEntities);
			mAdapter.setType(0);
			lv.setAdapter(mAdapter);
			mCustomerInfoEntity = (CustomerInfoEntity) getIntent().getParcelableExtra("entity");
			// 个性标签
			if (mCustomerInfoEntity.getType() == 0) {
				titleTextV.setText(mCustomerInfoEntity.getInterestName());
			} else {
				titleTextV.setText(mCustomerInfoEntity.getSameExperience());
			}
			Flag = "-100000";
			mCustomerInfoEntity.setFlag(Flag);
			FriendHttpUtil.getFriends(mCustomerInfoEntity, 0,SearchByTagActivity.this);

		} else if (object instanceof GroupInfoEntity) {
			mGroupInfoEntities = new ArrayList<GroupInfoEntity>();
			mAdapter = new SearchAdapter<GroupInfoEntity>(this,
					mGroupInfoEntities);
			lv.setAdapter(mAdapter);
			mGroupInfoEntity = (GroupInfoEntity) getIntent()
					.getParcelableExtra("entity");
			titleTextV.setText(mGroupInfoEntity.getInfoLayName());
			mGroupInfoEntity.setType(1);
			mGroupInfoEntity.setFlag(Flag);
			mGroupInfoEntity.setFlagPlacing(flagPlacing);
			SalonHttpUtil.getSalonInfoData(mGroupInfoEntity, 0,responseHandler);
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		initData(intent);
	}

	private void initWidget() {
		initTitle();
		mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.hall);
		lv = mPullToRefreshListView.getRefreshableView();
		lv.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		position = position - 1;
		if (object instanceof CustomerInfoEntity) {
			FriendHttpUtil.onItemClick(this,
					mCustomerInfoEntities.get(position));
		} else if (object instanceof GroupInfoEntity) {
			SalonHttpUtil.onItemClick(getApplicationContext(), this,
					getSupportFragmentManager(),
					mGroupInfoEntities.get(position), true);
		}
	}

	@SuppressWarnings("unchecked")
//	@Override
//	public void response(int sourceType, String content) {
//		boolean isFristPage = false;
//		if (sourceType == -1 || content.equals("")) {
//			if (!content.equals("")) {
//				ToastUtil.showShort(this, content);
//			}
//			LodingFragmentDialog.dismiss(getSupportFragmentManager());
//			mPullToRefreshListView.onRefreshComplete();
//			return;
//		}
//		if (flagPlacing.equals("-100000")) {
//			mGroupInfoEntities.clear();
//		} 
//		ArrayList<GroupInfoEntity> entities;
//		if ((entities = SalonHttpUtil.jsonAnalysisSalonEntity(content)).size()!= 0) {
//			mGroupInfoEntities.addAll(entities);
//			mAdapter.notifyDataSetChanged();
//			Flag = mAdapter.getPageFlagMark(isFristPage,object);
//			flagPlacing = mAdapter.getFlagPlacingMark(isFristPage,object);
//		}
//		LodingFragmentDialog.dismiss(getSupportFragmentManager());
//	}
	
ResponseHandler responseHandler=new ResponseHandler(0);
	
	class ResponseHandler extends ObjectHttpResponseHandler{
		int sortNum ;
		public ResponseHandler(int sort) {
			this.sortNum=sort;
		}
		
		public void onFinish() {
			super.onFinish();
			showLodingDialog.dismissAllowingStateLoss();
			mPullToRefreshListView.onRefreshComplete();
		};
		
		@Override
		public void onSuccess(Object response) {
			super.onSuccess(response);
			if(response==null)return;
			if (flagPlacing.equals("-100000")) {
				mGroupInfoEntities.clear();
			}
			 ArrayList<GroupInfoEntity> entities=(ArrayList<GroupInfoEntity>) response;
			 if(entities.size()==0){
				 ToastUtil.showToastPanl("暂无更多内容...");
				 return;
			 }
			 mGroupInfoEntities.addAll(entities);
				// //点击按钮重新刷新adapter并且指定第一个开始
			 mAdapter.notifyDataSetChanged();
			Flag = mAdapter.getPageFlagMark(false,object);
			flagPlacing = mAdapter.getFlagPlacingMark(false,object);
		}
		
		@Override
		public Object onParseResponse(String content) {
			if (!content.equals("N")) {
				return  SalonHttpUtil.jsonAnalysisSalonEntity(SearchByTagActivity.this,content);
			}else {
				return null;
			}
		}
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.head_image:
			int position = (Integer) v.getTag();
			Intent intent = new Intent();
			;
			if (object instanceof GroupInfoEntity) {
				if (mGroupInfoEntities.get(position).getCreateCustomerID()
						.equals(SmartFoxClient.getLoginUserId())) {
					intent.setClass(getApplicationContext(),SalonReadSelf.class);
					intent.putExtra("id", mGroupInfoEntities.get(position)
							.getId());
					startActivity(intent);
				} else {
					intent.setClass(getApplicationContext(),
							SalonInformationActivity.class);
					Bundle bundle = new Bundle();
					intent.putExtra("id", mGroupInfoEntities.get(position).getId());
					intent.putExtras(bundle);
					startActivity(intent);
				}
			} else if (object instanceof CustomerInfoEntity) {
				intent.setClass(getApplicationContext(), FriendInformUI.class);
				intent.putExtra("id", mCustomerInfoEntities.get(position).getId());
				startActivity(intent);
			}
		default:
			break;
		}
	}

	@Override
	public void responseSuccess(int type, int sourceType,
			ArrayList<CustomerInfoEntity> c) {
		boolean isFristPage;
		if (Flag.equals("-100000")) {
			mCustomerInfoEntities.clear();
		}
		isFristPage = false;
		if (c.size() != 0) {
			mCustomerInfoEntities.addAll(c);
			mAdapter.notifyDataSetChanged();
			Flag = mAdapter.getPageFlagMark(isFristPage,object);
		}
		mPullToRefreshListView.onRefreshComplete();
		LodingFragmentDialog.dismiss(getSupportFragmentManager());
	}

	@Override
	public void responseError(int type, int sourceType, final String content) {
		ToastUtil.showShort(getApplicationContext(), content);
		mCustomerInfoEntities.clear();
		mAdapter.notifyDataSetChanged();
		mPullToRefreshListView.onRefreshComplete();
		LodingFragmentDialog.dismiss(getSupportFragmentManager());
	}


	@Override
	public void onTicketHandler(String state, GroupInfoEntity entity) {
		if ("0".equals(state)) {
//			ToastUtil.showShort(getApplicationContext(), "多美币不足");
		} else if ("-1".equals(state)) {
			ToastUtil.showShort(getApplicationContext(), "服务器出错");
		} else {
			Intent intent1 = new Intent();
			intent1.putExtra(ChatActivity.KEY_PARAME, entity);
			intent1.setClass(getApplicationContext(), ChatActivity.class);
			startActivity(intent1);
		}
	}

}
